// 导入request模块
import request from './request'
import { isMockMode } from '../config';

// 患者查看自己的所有评估记录
export const getAssessments = (page = 1, size = 10) => {
  return request.get('/patient/assessments', {
    page,
    size
  })
}

// 患者查看某次评估的量表详情
export const getAssessmentDetails = (assessmentId) => {
  return request.get(`/patient/assessments/${assessmentId}/details`)
}

// 患者更新手机号
export const updatePhone = (phone) => {
  return request.post('/auth/update-phone', {
    phone
  })
}

// 提交量表评估
export const submitScaleAssessment = (scaleType, answers) => {
  return request.post(`/patient/assessments/submit/${scaleType}`, {
    answers
  })
}

// 获取最近一次评估结果
export const getLatestAssessment = () => {
  return request.get('/patient/assessments/latest')
}

// 患者自主建档
export const patientSelfRegister = (patientInfo) => {
  return request.post('/users/patient/self-register', patientInfo)
}

/**
 * 患者登录
 * @param {Object} params - 登录参数
 * @param {Object} params.user_info - 用户信息对象
 * @returns {Promise<Object>} 登录结果
 */
export default async function patientLogin(params = {}) {
  try {
    // 提取用户信息
    const { user_info = {} } = params;
    
    // 验证必填字段
    if (!user_info.name || !user_info.phone || !user_info.id_card) {
      throw new Error('患者登录信息不完整：姓名、手机号和身份证号为必填项');
    }
    
    // 构造请求数据
    const requestData = {
      user_info: {
        name: user_info.name,
        phone: user_info.phone,
        id_card: user_info.id_card,
        age: user_info.age,
        gender: user_info.gender || 'male'
      }
    };
    
    console.log('患者登录请求数据:', requestData);
    
    if (isMockMode) {
      // 模拟登录响应
      return {
        data: {
          token: 'mock_patient_token_' + Date.now(),
          user_info: {
            id: '1',
            name: user_info.name,
            phone: user_info.phone,
            id_card: user_info.id_card,
            age: user_info.age,
            gender: user_info.gender || 'male',
            user_type: 'patient',
            role: 'patient'
          }
        }
      };
    }
    
    // 调用实际登录接口
    const response = await request.post('/auth/patient/login', requestData);
    return response;
  } catch (error) {
    console.error('患者登录失败:', error);
    throw error;
  }
}